package com.company.Thread.Concurrent;

import java.util.concurrent.TimeUnit;

/**
 * 关于锁的8个问题
 * */
public class lock4 {
    public static void main(String[] args) {
        Phone2 phone1 = new Phone2();
        Phone2 phone2 = new Phone2();

        new Thread(()->{
            phone1.send();
        }, "A").start();

        try {
            TimeUnit.SECONDS.sleep(1);
        }catch (InterruptedException e){
            e.printStackTrace();
        }

        new Thread(()->{
            phone1.call();
        }, "B").start();

        new Thread(()->{
            phone1 .email();
        }, "C").start();
    }
}

class Phone2{
    // synchronized 锁的对象是方法的调用者
    public synchronized void send(){
        try {
            TimeUnit.SECONDS.sleep(3);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        System.out.println("发短信");
    }

    public synchronized void call(){
        System.out.println("打电话");
    }

    public void email(){
        System.out.println("发邮箱");
    }
}

